#define _CRT_SECURE_NO_WARNINGS 1

#include "Date.h"


// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
// 析构函数
Date::~Date()
{
	_year = _month = _day = 0;
}
// 拷贝构造函数
// d2(d1)
Date::Date(/*Date* this*/ const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
}



// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	int array[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
	{
		array[2] = 29;
	}

	return array[month];
}

// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(/*Date* this*/const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;

	return *this;
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;

	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;

		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
	while (1)
	{
		int num = Date::GetMonthDay(_year, _month);
		if (_day - num >= 0)
		{
			_day -= num;
			_month++;
		}
		else
		{
			break;
		}
		if (_month >= 12)
			_month++;
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (day <= 0)
	{
		if (_month == 1)
		{
			_year--;
			_month = 13;
		}
		_day += GetMonthDay(_year, _month - 1);
		_month--;

		if (_day == 0)
		{
			_month--;
			_day = GetMonthDay(_year, _month);
		}
	}
	return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

// 前置++
Date& Date::operator++()
{
	_day++;
	if (_day > GetMonthDay(_year, _month))
	{
		_day = 1;
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	return (*this)++;
}
// 后置--
Date Date::operator--(int)
{
	return (*this)--;
}
// 前置--
Date& Date::operator--()
{
	_day--;
	if (_day == 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day = GetMonthDay(_year, _month);
	}
	return *this;
}

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// >=运算符重载
bool Date::operator >= (const Date& d)
{
	return *this > d || *this == d;
}

// <运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	assert(_year > d._year
		|| (_year == d._year) && (_month > d._month)
		|| (_year == d._year) && (_month == d._month) && (_day >= d._day));

	Date tmp(*this);
}